<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/12/17
 * Time: 15:30
 */

namespace app\admin\controller;

use think\Db;
use think\Controller;
use app\admin\model\System as systemModel;


class System extends Controller
{

    public function index()
    {
        $model = new systemModel();

        $post = $this->request->post();
        if (isset($post['name']) and !empty($post['name'])) {
            $where['name'] = ['like', '%' . $post['name'] . '%'];
        }

        $attachment = empty($where) ? $model->order('id desc')->paginate(20) : $model->where($where)->order('id desc')->paginate(20);

        $this->assign('system',$attachment);
        return $this->fetch();
    }

    public function publish()
    {
        //获取菜单id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new systemModel();
        //是正常添加操作
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['name', 'require', '姓名不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证菜单是否存在
                $system = $model->where('id',$id)->find();
                if(empty($system)) {
                    return $this->error('id不正确');
                }
                if(false == $model->allowField(true)->save($post,['id'=>$id])) {
                    return $this->error('修改失败');
                } else {
                    $this->success('修改成功','admin/system/index');
                }
            } else {
                //非提交操作
                $system = $model->where('id',$id)->find();

                $this->assign('system',$system);
                if(!empty($system)) {
                    $this->assign('system',$system);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字d段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['name', 'require', '姓名不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
                    return $this->success('添加成功','admin/system/index');
                }
            } else {
                //非提交操作
                $id = $this->request->has('id') ? $this->request->param('id', null, 'intval') : null;
                if(!empty($id)) {
                    $this->assign('id',$id);
                }
                $system = $model->select('id',$id);
                $this->assign('system',$system);
                return $this->fetch();
            }

        }
    }


    //删除
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('system')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                return $this->success('删除成功','admin/system/index');
            }
        }
    }

}